from utils.RedisUtil import *
import requests
import json
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from matplotlib.ticker import MultipleLocator
import datetime

#codes = ['SZ000595','SZ300925','SZ000158','SH600830','SH688200','SZ300561','SZ300663','SZ000995','SZ000717','SZ000595']
codes = ['SZ000158']

redis = RedisUtil()
for code in codes:
    print(code[2:])
    key = "data_" + str(code[2:])
    al = redis.vget(key)
    if al != None:
        print("read cache")
        data = []
        pre_close = 1
        for row in json.loads(al):
            # if row[0] != find_day:
            #    continue
            info = {
                "code": code,
                "trade_date": row[0],
                "open": row[1],
                "high": row[6],
                "low": row[5],
                "close": row[2],
                "volume": round(float(row[7]) * 100/100000000,2),
                "money": round(float(row[8]) * 10000/100000000,2)
            }
            data.append(info)
        df = pd.DataFrame(data)
        df.set_index("trade_date",inplace=True)
        df_sorted = df.sort_index(ascending=True)
        newdata = []
        pre_close = 1
        i = 0
        base_price = 0
        base_vol = 0
        close_data = []
        x_data = []
        dis_close_rate = []
        dis_vol_rate = []
        for index, row in df_sorted.iterrows():
            if i == 0:
                base_price = row["close"]
                base_vol = row["volume"]
            info = {
                "code": row["code"],
                "trade_date": index,
                "open": row["open"],
                "high": row["high"],
                "low": row["low"],
                "close": row["close"],
                "volume": row["volume"],
                "money": row["money"],
                "preclose": pre_close,
                "zf": round((float(row["close"])-float(pre_close))*100/float(pre_close),2),
                "dis_close": float(row["close"]) - float(base_price),
                "dis_close_rate": round((float(row["close"]) - float(base_price))/float(base_price),2),
                "dis_vol": float(row["volume"]) - float(base_vol),
                "dis_vol_rate": round((float(row["volume"]) - float(base_vol))/float(base_vol),2)
            }
            x_data.append(index)
            close_data.append(row["close"])
            dis_close_rate.append(info["dis_close_rate"]*20)
            dis_vol_rate.append(info["dis_vol_rate"])
            pre_close = row["close"]
            newdata.append(info)
            i += 1
        newdfdata = pd.DataFrame(newdata)
        print(newdfdata)

        xs = [datetime.datetime.strptime(d, '%Y-%m-%d').date() for d in x_data]

        # 创建图像和轴
        fig, ax = plt.subplots()

        # 绘制第一条折线
        ax.plot(xs, close_data, label='close')

        # 绘制第二条折线
        ax.plot(xs, dis_close_rate, label='dis_close_rate')

        # 绘制第二条折线
        ax.plot(xs, dis_vol_rate, label='dis_vol_rate')

        plt.tick_params(axis='both', which='both', labelsize=10)

        # 显示折线图
        plt.gcf().autofmt_xdate()  # 自动旋转日期标记

        # 创建Locator对象
        y_locator = MultipleLocator(2)

        # 设置y轴刻度间隔
        plt.gca().yaxis.set_major_locator(y_locator)

        # 显示图例
        ax.legend()

        # 显示图像
        plt.show()
        break
    else:
        continue
    break



